package com.android.lovely.ui.fragment

import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.lovely.R
import com.android.lovely.data.SearchPopular
import com.android.lovely.data.SearchRecommend
import com.android.lovely.databinding.FragmentSearchRecommendBinding
import com.android.lovely.framework.BaseFragment
import com.android.lovely.framework.BaseMvvmFragment
import com.android.lovely.room.search.SearchDatabase
import com.android.lovely.room.search.SearchEntity
import com.android.lovely.ui.adapter.SearchAdapter
import com.android.lovely.ui.adapter.SearchPopularAdapter
import com.android.lovely.ui.adapter.onTagClickListener
import com.android.lovely.ui.dialog.LovelyDialog
import com.android.lovely.ui.dialog.showLovelyDialog
import com.android.lovely.ui.viewmodel.HomeViewModel
import com.android.lovely.ui.viewmodel.SearchViewModel
import com.android.lovely.user.UserManager
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport.Session.User
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import kotlinx.coroutines.flow.collectLatest

/**
 *
 * @author why
 * @date 2025/3/13 14:21
 */
class SearchRecommendFragment :
    BaseFragment<FragmentSearchRecommendBinding>() {
    private val mViewModel by activityViewModels<SearchViewModel>()


    private val historyAdapter by lazy {
        SearchAdapter()
    }

    private val popularAdapter by lazy {
        SearchPopularAdapter()
    }

    private val adapter by lazy {
        ConcatAdapter()
    }

    override fun initView() {
        historyAdapter.setOnTagClickListener(object : onTagClickListener {
            override fun onTagClick(tag: String) {
                mViewModel.search(tag)
            }

            override fun removeAll() {
                requireActivity().showLovelyDialog {
                    setDialogTitle(getString(R.string.delete_search))
                    setDialogMessage(getString(R.string.delete_search_history))
                    setPositiveButton {
                        viewLifecycleOwner.lifecycleScope.launchSafety {
                            SearchDatabase.dao.deleteAll(UserManager.authId)
                            adapter.removeAdapter(historyAdapter)
                        }
                    }
                }
            }
        })
        popularAdapter.setOnTagClickListener(object : onTagClickListener {
            override fun onTagClick(tag: String) {
                viewLifecycleOwner.lifecycleScope.launchSafety {
                    SearchDatabase.dao.insert(SearchEntity(tag))
                }
                mViewModel.search(tag)
            }

            override fun removeAll() {

            }
        })
        adapter.addAdapter(historyAdapter)
        adapter.addAdapter(popularAdapter)
        mBinding.rv.layoutManager = LinearLayoutManager(context)
        mBinding.rv.adapter = adapter
        viewLifecycleOwner.lifecycleScope.launchSafety {
            SearchDatabase.dao.queryAll(UserManager.authId).collectLatest {
                historyAdapter.updateList(arrayListOf(SearchRecommend(it)))
                if (it.isNotEmpty()) {
                    adapter.addAdapter(0, historyAdapter)
                } else {
                    adapter.removeAdapter(historyAdapter)
                }
            }
        }
        mViewModel.getHotKeys()
        viewLifecycleOwner.lifecycleScope.launchSafety {
            mViewModel.popularFlow.collectLatest {
                popularAdapter.updateList(arrayListOf(SearchPopular(it)))
            }
        }
    }

    override suspend fun initData() {

    }
}